import 'dart:math';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';
import 'package:ls_work/a_yyt/models/floor_list_entity.dart';

import '../res/colors.dart';
import '../viewmodel/select_room_view_model.dart';
import '../widgets/drop_item_widget.dart';

class SelectRoomPage extends BaseStatefulMvvmWidget{
  @override
  State<StatefulWidget> createState() => _SelectRoomState();
}

class _SelectRoomState extends BaseMvvmRefreshState<SelectRoomViewModel,SelectRoomPage>{

  GZXDropdownMenuController _dropdownMenuController =
  GZXDropdownMenuController();

  GlobalKey _stackKey = GlobalKey();
  

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(title: Text('选择房间'),),
      body: Stack(
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              GZXDropDownHeader(
                // 下拉的头部项，目前每一项，只能自定义显示的文字、图标、图标大小修改
                items: [
                  GZXDropDownHeaderItem(
                      viewModel.selectBuilding?.buildingNo ?? "全部"),
                  GZXDropDownHeaderItem(viewModel.selectFloor?.floorNo ?? "全部"),
                ],
                // GZXDropDownHeader对应第一父级Stack的key
                stackKey: _stackKey,
                // controller用于控制menu的显示或隐藏
                controller: _dropdownMenuController,
                height: 50,
                color: Colours.bg_color,
                borderColor: Colours.bg_color,
                dividerHeight: 20,
                dividerColor: Colours.order_line,
                style: TextStyle(color: Color(0xFF666666), fontSize: 14),
                dropDownStyle: TextStyle(
                  fontSize: 14,
                  color: Colours.app_main,
                ),
                iconDropDownColor: Colours.app_main,
              ),
              loadingContentWidget.setWeight(1)
            ],
          ),

          // 下拉菜单
          GZXDropDownMenu(
            // controller用于控制menu的显示或隐藏
            controller: _dropdownMenuController,
            // 下拉菜单显示或隐藏动画时长
            animationMilliseconds: 300,
            dropdownMenuChanging: (isShow, index) {},
            dropdownMenuChanged: (isShow, index) {},
            menus: [
              if((viewModel.buildingList??[]).isNotEmpty)
                GZXDropdownMenuBuilder(
                  dropDownHeight: min(40.0 * (viewModel.buildingList??[]).length,
                      MediaQuery.of(context).size.height * 0.75),
                  dropDownWidget:
                  buildConditionListWidget(viewModel.buildingList??[], (data) {
                    _dropdownMenuController.hide();
                    setState(() {
                      viewModel.selectBuilding = data as FloorEntity?;
                      viewModel.setFloor();
                    });
                    viewModel.requestRefresh();
                  }),
                ),
              if((viewModel.floorList??[]).isNotEmpty)
                GZXDropdownMenuBuilder(
                  dropDownHeight: min(40.0 * (viewModel.floorList??[]).length,
                      MediaQuery.of(context).size.height * 0.75),
                  dropDownWidget:
                  buildConditionListWidget(viewModel.floorList??[], (value) {
                    _dropdownMenuController.hide();
                    setState(() {
                      viewModel.selectFloor = value as FloorInfo?;
                    });
                    viewModel.requestRefresh();
                  }),
                ),
            ],
          ),
        ],
        key: _stackKey,
      ),
    );
  }

  @override
  SelectRoomViewModel createViewModel() {
    return SelectRoomViewModel(context);
  }

  @override
  void onRetryClick() {
    viewModel.requestListData();
  }

  @override
  Widget createScrollWidget() {
    return GridView.count(crossAxisCount: 4,mainAxisSpacing: 10,crossAxisSpacing: 10,children: viewModel.listItems.map((e){
      return Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          CommonText("${e.roomNo}",textSize: 17),
          Container(height: 10,),
          // CommonText(e.buildingNo,textColor: Colors.grey,textSize: 14)
        ],
      ).onTap(() {
        var address = "${e.buildingNo}${e.floorNo}${e.roomNo}";
        Go().pop(result: address);
      }).addToContainer(
        decoration: BoxDecoration(
          color: Colors.green[100],
          borderRadius: 10.borderRadius
        ),
        padding: EdgeInsets.all(8)
      );
    }).toList(),padding: EdgeInsets.symmetric(horizontal: 16,vertical: 10),);
  }

}